{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "AMD-Predictions.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": [],
      "toc_visible": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "metadata": {
        "id": "JsxPKAwsG3pk",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "import librosa\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "import pickle\n",
        "import os"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "TlUeV0ng4QRI",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        ""
      ]
    },
    {
      "metadata": {
        "id": "94pJBvsdG8SD",
        "colab_type": "code",
        "outputId": "17af5153-08ec-4c6c-dc4e-1ef0e5ea98a7",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 54
        }
      },
      "cell_type": "code",
      "source": [
        "from google.colab import drive\n",
        "\n",
        "# This will prompt for authorization.\n",
        "drive.mount('/content/drive')"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "4EOatbXKriNr",
        "colab_type": "code",
        "outputId": "232b8d52-fd74-4565-9393-88b76e9f9e7c",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 136
        }
      },
      "cell_type": "code",
      "source": [
        "from sklearn import metrics\n",
        "from sklearn.externals import joblib\n",
        "\n",
        "RECORDINGS_PATH = \"/content/drive/My Drive/amd-training\"\n",
        "os.chdir(RECORDINGS_PATH)\n",
        "\n",
        "# loaded_model = pickle.load(open(\"rf-20190125T2102.pkl\", \"rb\"))\n",
        "loaded_model = joblib.load(\"rf-20190129T1549.pkl\")\n",
        "\n",
        "loaded_model"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
              "            max_depth=None, max_features='auto', max_leaf_nodes=None,\n",
              "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
              "            min_samples_leaf=1, min_samples_split=2,\n",
              "            min_weight_fraction_leaf=0.0, n_estimators=100, n_jobs=None,\n",
              "            oob_score=False, random_state=None, verbose=0,\n",
              "            warm_start=False)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 50
        }
      ]
    },
    {
      "metadata": {
        "id": "aMTqu79X8Zng",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Testing"
      ]
    },
    {
      "metadata": {
        "id": "OkMAXTBV-lUa",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "def predictFromFile(file):\n",
        "  print(file)\n",
        "  X, sample_rate = librosa.load(file, res_type='kaiser_fast') \n",
        "  mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)\n",
        "  X = [mfccs]\n",
        "  print(\"predict {}\".format(loaded_model.predict(X)))\n",
        "  print(\"predict_proba {}\".format(loaded_model.predict_proba(X)))\n",
        "\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "0tO4zOlI0oXp",
        "colab_type": "code",
        "outputId": "138d7815-5f20-469c-c54c-94cec0ceb1c5",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1904
        }
      },
      "cell_type": "code",
      "source": [
        "import glob\n",
        "for file in glob.glob(\"sample-files/*\"):\n",
        "  predictFromFile(file)"
      ],
      "execution_count": 0,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "sample-files/rec-838c2f2ae3ba4e72b5ec1846d42e216f-20190129T125903.wav\n",
            "predict [0]\n",
            "predict_proba [[0.59 0.41]]\n",
            "sample-files/rec-838c2f2ae3ba4e72b5ec1846d42e216f-20190129T125848.wav\n",
            "predict [0]\n",
            "predict_proba [[0.74 0.26]]\n",
            "sample-files/rec-838c2f2ae3ba4e72b5ec1846d42e216f-20190129T125856.wav\n",
            "predict [0]\n",
            "predict_proba [[0.51 0.49]]\n",
            "sample-files/rec-838c2f2ae3ba4e72b5ec1846d42e216f-20190129T125904-beep.wav\n",
            "predict [0]\n",
            "predict_proba [[0.86 0.14]]\n",
            "sample-files/rec-838c2f2ae3ba4e72b5ec1846d42e216f-20190129T134024-beep.wav\n",
            "predict [0]\n",
            "predict_proba [[1. 0.]]\n",
            "sample-files/rec-838c2f2ae3ba4e72b5ec1846d42e216f-20190129T134023.wav\n",
            "predict [1]\n",
            "predict_proba [[0.46 0.54]]\n",
            "sample-files/rec-838c2f2ae3ba4e72b5ec1846d42e216f-20190129T134057.wav\n",
            "predict [1]\n",
            "predict_proba [[0.44 0.56]]\n",
            "sample-files/rec-838c2f2ae3ba4e72b5ec1846d42e216f-20190129T134058-beep.wav\n",
            "predict [0]\n",
            "predict_proba [[1. 0.]]\n",
            "sample-files/rec-5017b54e7b76476098786a4112d9463b-20190129T134059-beep.wav\n",
            "predict [0]\n",
            "predict_proba [[1. 0.]]\n",
            "sample-files/rec-5017b54e7b76476098786a4112d9463b-20190129T134025-speech-beep.wav\n",
            "predict [1]\n",
            "predict_proba [[0.41 0.59]]\n",
            "sample-files/rec-d44b0840172545e38a8d5ef82cf67c67-20190129T173246.wav\n",
            "predict [0]\n",
            "predict_proba [[0.66 0.34]]\n",
            "sample-files/rec-8d94142f293c4a649c24da22ed5a7838-20190129T172221.wav\n",
            "predict [0]\n",
            "predict_proba [[0.52 0.48]]\n",
            "sample-files/rec-148499aac7ce4f54b94e027977388e2b-20190129T172329.wav\n",
            "predict [1]\n",
            "predict_proba [[0.47 0.53]]\n",
            "sample-files/rec-d44b0840172545e38a8d5ef82cf67c67-20190129T173210.wav\n",
            "predict [0]\n",
            "predict_proba [[0.66 0.34]]\n",
            "sample-files/rec-8d94142f293c4a649c24da22ed5a7838-20190129T172236.wav\n",
            "predict [0]\n",
            "predict_proba [[0.56 0.44]]\n",
            "sample-files/rec-8d94142f293c4a649c24da22ed5a7838-20190129T172330.wav\n",
            "predict [1]\n",
            "predict_proba [[0.47 0.53]]\n",
            "sample-files/rec-c96ee63d346b4c678eb4f57721197398-20190129T160000-beep.wav\n",
            "predict [1]\n",
            "predict_proba [[0.38 0.62]]\n",
            "sample-files/rec-d44b0840172545e38a8d5ef82cf67c67-20190129T173156.wav\n",
            "predict [0]\n",
            "predict_proba [[0.59 0.41]]\n",
            "sample-files/rec-148499aac7ce4f54b94e027977388e2b-20190129T172338.wav\n",
            "predict [0]\n",
            "predict_proba [[0.55 0.45]]\n",
            "sample-files/rec-8d94142f293c4a649c24da22ed5a7838-20190129T172237.wav\n",
            "predict [0]\n",
            "predict_proba [[0.57 0.43]]\n",
            "sample-files/rec-c96ee63d346b4c678eb4f57721197398-20190129T160017.wav\n",
            "predict [0]\n",
            "predict_proba [[0.8 0.2]]\n",
            "sample-files/rec-c96ee63d346b4c678eb4f57721197398-20190129T160023.wav\n",
            "predict [0]\n",
            "predict_proba [[0.8 0.2]]\n",
            "sample-files/rec-c96ee63d346b4c678eb4f57721197398-20190129T160336-beep.wav\n",
            "predict [0]\n",
            "predict_proba [[0.95 0.05]]\n",
            "sample-files/rec-d44b0840172545e38a8d5ef82cf67c67-20190129T173149.wav\n",
            "predict [0]\n",
            "predict_proba [[0.51 0.49]]\n",
            "sample-files/rec-c96ee63d346b4c678eb4f57721197398-20190129T160033.wav\n",
            "predict [1]\n",
            "predict_proba [[0.49 0.51]]\n",
            "sample-files/rec-d44b0840172545e38a8d5ef82cf67c67-20190129T173141.wav\n",
            "predict [0]\n",
            "predict_proba [[0.71 0.29]]\n",
            "sample-files/rec-d44b0840172545e38a8d5ef82cf67c67-20190129T173223.wav\n",
            "predict [0]\n",
            "predict_proba [[0.68 0.32]]\n",
            "sample-files/rec-d44b0840172545e38a8d5ef82cf67c67-20190129T173236.wav\n",
            "predict [0]\n",
            "predict_proba [[0.68 0.32]]\n",
            "sample-files/rec-c96ee63d346b4c678eb4f57721197398-20190129T160034-beep.wav\n",
            "predict [0]\n",
            "predict_proba [[1. 0.]]\n",
            "sample-files/rec-148499aac7ce4f54b94e027977388e2b-20190129T172315.wav\n",
            "predict [0]\n",
            "predict_proba [[0.72 0.28]]\n",
            "sample-files/rec-8d94142f293c4a649c24da22ed5a7838-20190129T172233.wav\n",
            "predict [1]\n",
            "predict_proba [[0.42 0.58]]\n",
            "sample-files/rec-8d94142f293c4a649c24da22ed5a7838-20190129T172256.wav\n",
            "predict [0]\n",
            "predict_proba [[0.55 0.45]]\n",
            "sample-files/rec-8d94142f293c4a649c24da22ed5a7838-20190129T172314.wav\n",
            "predict [0]\n",
            "predict_proba [[0.55 0.45]]\n",
            "sample-files/rec-c96ee63d346b4c678eb4f57721197398-20190129T160011.wav\n",
            "predict [0]\n",
            "predict_proba [[0.8 0.2]]\n",
            "sample-files/rec-c96ee63d346b4c678eb4f57721197398-20190129T160005.wav\n",
            "predict [0]\n",
            "predict_proba [[0.79 0.21]]\n",
            "sample-files/rec-8d94142f293c4a649c24da22ed5a7838-20190129T172339.wav\n",
            "predict [0]\n",
            "predict_proba [[0.55 0.45]]\n",
            "sample-files/rec-8d94142f293c4a649c24da22ed5a7838-20190129T172213.wav\n",
            "predict [0]\n",
            "predict_proba [[0.7 0.3]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "1ed99Y6otyds",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}